package com.roger.myHome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.roger.myHome.domain.TbExpenses;
import com.roger.myHome.domain.TbTotalExpense;
import com.roger.myHome.entity.dto.ExpensesAddReq;
import com.roger.myHome.entity.qto.ExpensesQry;
import com.roger.myHome.entity.vo.ExpensesVo;
import com.roger.myHome.mapper.ExpensesMapper;
import com.roger.myHome.mapper.TotalExpenseMapper;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.ExpensesService;
import com.roger.myHome.util.DateUtil;
import com.roger.myHome.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ExpensesServiceImpl implements ExpensesService {

    @Resource
    private ExpensesMapper expensesMapper;

    @Resource
    private TotalExpenseMapper totalExpenseMapper;

    @Override
    public EUDataGridResult page(ExpensesQry qryReq) {
        List<ExpensesVo> list = expensesMapper.page(qryReq);

        return new EUDataGridResult(list, expensesMapper.totalSize(qryReq));
    }

    @Override
    public String add(ExpensesAddReq expensesAddReq) {
        TbExpenses tbExpenses = new TbExpenses();
        Map<String, String> hashMap = new HashMap();
        try {
            BeanUtils.copyProperties(expensesAddReq,tbExpenses);
            tbExpenses.setPayTime(DateUtil.strToDate(expensesAddReq.getPayTimeString()));
            StringBuffer sb = new StringBuffer(expensesAddReq.getCountry());
            sb.append("-")
                    .append(expensesAddReq.getProvince())
                    .append("-").append(expensesAddReq.getCity())
                    .append("-")
                    .append(expensesAddReq.getAddress());
            tbExpenses.setFullAddress(sb.toString());
            expensesMapper.insert(tbExpenses);
            //累加报表
            //1、查出当月金额
            Integer[] array = DateUtil.formatDateString(expensesAddReq.getPayTimeString());
            int year = array[0];
            int m = array[1];
            QueryWrapper<TbTotalExpense> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year",year).eq("month",m).eq("user_id",expensesAddReq.getCreateUser());
            TbTotalExpense tbTotalExpense = totalExpenseMapper.selectList(queryWrapper).get(0);
            //2、更新

            BigDecimal totalMoney = tbTotalExpense.getTotalMoney().add(expensesAddReq.getPayMoney());

            LambdaUpdateWrapper<TbTotalExpense> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(TbTotalExpense::getId , tbTotalExpense.getId())
                    .set(TbTotalExpense::getTotalMoney, totalMoney);
            totalExpenseMapper.update(null, lambdaUpdateWrapper);

            hashMap.put("result", "新增成功!");

        } catch (Exception e) {
            hashMap.put("result", "新增失败!");
        }
        return StringUtils.getJSONString(hashMap);
    }

    @Override
    public String delete(Long userId,Long id) {
        TbExpenses obj = expensesMapper.selectById(id);
        String payTime = DateUtil.dateToString(obj.getPayTime(), "yyyy-MM-dd HH:mm:ss");
        Integer[] array = DateUtil.formatDateString(payTime);
        int year = array[0];
        int m = array[1];
        QueryWrapper<TbTotalExpense> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year).eq("month",m).eq("user_id",userId);
        TbTotalExpense tbTotalExpense = totalExpenseMapper.selectList(queryWrapper).get(0);
        BigDecimal totalMoney = tbTotalExpense.getTotalMoney().subtract(obj.getPayMoney());

        LambdaUpdateWrapper<TbTotalExpense> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TbTotalExpense::getId , tbTotalExpense.getId())
                .set(TbTotalExpense::getTotalMoney, totalMoney);
        totalExpenseMapper.update(null, lambdaUpdateWrapper);

        expensesMapper.deleteById(id);
        Map<String , String> hashMap = new HashMap();
        hashMap.put("result", "删除成功!");
        return StringUtils.getJSONString(hashMap);
    }

    @Override
    public String searchExpenseByDate(ExpensesQry qryReq) {
        Map<String, String> hashMap = new HashMap();
        BigDecimal d = expensesMapper.searchExpenseByDate(qryReq);
        if (d == null) {
            d = new BigDecimal(0);
        }
        hashMap.put("result", qryReq.getPayDate1() + "至" + qryReq.getPayDate2() + "共消费支出:" +
                d + "元");
        return StringUtils.getJSONString(hashMap);
    }

}
